// pages/ucenter/my-wait/my-wait.js
const app = getApp();
import * as API_Members from '../../api/members'
import * as API_Distribution from '../../api/distribution'
import { Foundation } from '../../ui-utils/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fundList:[],
    params: {
      page_no: 1,
      page_size: 20,
      is_withdrawr: 0,
      member_id: ''
    },
    finished: false, 
    showGoTop: false,
    scrollTop: 0,//滚动高度
    scrollHeight: '',
    pageCount: 0,
    msg: '',
    typeName:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight
    })

  },
  getList(){
    const userInfo = wx.getStorageSync('user');
    console.log(userInfo);
    this.setData({'params.member_id':userInfo.member_id})
    API_Distribution.getAllFenRun(this.data.params).then(response=>{
      let _data = response.data;
      let _pageCount = Math.ceil(response.data_total / this.data.params.page_size)
      this.setData({ pageCount: _pageCount })
      if(_data && _data.length){
        let fenrun = [];
        _data.forEach(item=>{
            if (userInfo.uname == item.member_name) {
              if (item.price > 0) {
                fenrun.push({
                  price: item.price,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'推广分润'
                });
              }
              if (item.price_lv > 0) {
                fenrun.push({
                  price_lv: item.price_lv,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'个人养老'
                });
              }
            }else{
              if (item.price > 0) {
                fenrun.push({
                  price: item.price,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'推广分润'
                });
              }
              if (item.price_lv > 0) {
                fenrun.push({
                  price_lv: item.price_lv,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'个人养老'
                });
              }

              if (item.price_lv1 > 0) {
                fenrun.push({
                  price_lv1: item.price_lv1,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'私域养老'
                });
              }
              if (item.price_lv2 > 0) {
                fenrun.push({
                  price_lv2: item.price_lv2,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'公域养老'
                });
              }
              if (item.shop_price > 0) {
                fenrun.push({
                  shop_price: item.shop_price,
                  create_time: Foundation.unixToDate(item.create_time, 'yyyy-MM-dd'),
                  typeName:'店铺推荐人分润'
                });
              }
            }
        });
        this.setData({
          fundList:this.data.fundList.concat(fenrun)
        })
      }else{
        this.setData({ finished: true })
      }
      // console.log(this.data.fundList)
      // this.funList = this.funList.concat(fenrun);
    })
  },
  loadMore: function () {
    console.log(this.data.pageCount,'this.data.pageCount');
    if (!this.data.finshed) {
      this.setData({ "params.page_no": this.data.params.page_no + 1 })
      if (this.data.pageCount >= this.data.params.page_no) {
        this.getList();
      } else {
        this.setData({ msg: '已经到底了~' })
      }
    }
  },
  scroll: function (e) {
    console.log(11111)
    let that = this
    if (e.detail.scrollTop > 200) {
      that.setData({showGoTop: true})
    } else {
      that.setData({showGoTop: false})
    }
  },
  //返回顶部
  goTop: function () { this.setData({ scrollTop: 0 })},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})